//
//  Users.m
//  QcMedical1
//
//  Created by ios001 on 2016/12/25.
//  Copyright © 2016年 jiawen Du. All rights reserved.
//

#import "Users.h"
#import <objc/runtime.h>

@implementation Users

+ (instancetype)userWithDic:(NSDictionary *)dic{
    Users *user = [[Users alloc] init];
    
    [user setValuesForKeysWithDictionary:dic];
    
    return user;
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
    
}

//解档
- (id)initWithCoder:(NSCoder *)decoder{
    
    if (self = [super init]) {
        u_int count;
        
        //获取本类所有的成员变量
        Ivar *ivars = class_copyIvarList([self class], &count);
        for (u_int i = 0; i < count; i++) {
            //取出对应的成员变量
            Ivar ivar = ivars[i];
            
            const char *keyChar = ivar_getName(ivar);
            //解档
            NSString *keyStr = [NSString stringWithUTF8String:keyChar];
            [self setValue:[decoder decodeObjectForKey:keyStr] forKey:keyStr];
            
        }
        free(ivars);
    }
    return self;
}


//归档
- (void)encodeWithCoder:(NSCoder *)encoder{
    
    unsigned int count = 0;
    //获取本类所有的成员变量
    Ivar *ivars = class_copyIvarList([self class], &count);
    //遍历所有的成员变量
    for (int i = 0; i < count; i++) {
        //取出对应位置的成员变量
        Ivar ivar = ivars[i];
        //查看成员变量
        const char *keyChar = ivar_getName(ivar);
        //归档
        NSString *keyStr = [NSString stringWithUTF8String:keyChar];
        [encoder encodeObject:[self valueForKey:keyStr] forKey:keyStr];
        
    }
    free(ivars);
}


//解档
+ (Users *)unarchiver{
    NSString* filePath = [[NSBundle mainBundle] pathForResource:@"Users.plist" ofType:nil];
    
    Users* currentUser = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    return currentUser;
}

//存档
+ (void)archiver:(Users *)users{
    NSString* filePath = [[NSBundle mainBundle] pathForResource:@"Users.plist" ofType:nil];
    
    [NSKeyedArchiver archiveRootObject:users toFile:filePath];
}

//+ (NSString *)fileCachePath {
//    NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
//    return [cachePath stringByAppendingPathComponent:@"user.data"];
//}


@end
